package com.example.its2021606134chh.BusModule;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.its2021606134chh.R;

import java.util.List;

/**
 * @ClassName: BusListViewAdapter$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 20/8/2021$
 * @Version: 1.0
 */
public class BusListViewAdapter extends BaseAdapter {
    private Context context;
    private List<String> mDates;
    private LayoutInflater mLayoutInflater;
    private int lastClickPosition=-1;


    public BusListViewAdapter(Context context, List mDates) {
        this.context = context;
        this.mDates = mDates;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void singleChoose(int position){
        lastClickPosition = position;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDates==null?0:mDates.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_rv_route,parent,false);
            viewHolder.tvItemRouteId = convertView.findViewById(R.id.tvItemRouteId);
            viewHolder.tvItemRouteName = convertView.findViewById(R.id.tvItemRouteName);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tvItemRouteId.setText(String.valueOf(position + 1));
        viewHolder.tvItemRouteName.setText(mDates.get(position));

        if(position==lastClickPosition){
            viewHolder.tvItemRouteId.setBackgroundColor(Color.parseColor("#FF0000"));   //red
            viewHolder.tvItemRouteId.setTextColor(Color.parseColor("#FFFFFF")); //withe
            viewHolder.tvItemRouteName.setTextColor(Color.parseColor("#FF0000"));
        }else {
            viewHolder.tvItemRouteId.setBackgroundColor(Color.parseColor("#FFFFFF"));   //withe
            viewHolder.tvItemRouteId.setTextColor(Color.parseColor("#000000")); //black
            viewHolder.tvItemRouteName.setTextColor(Color.parseColor("#000000"));
        }

        return convertView;
    }

    static class ViewHolder{
        private TextView tvItemRouteId, tvItemRouteName;
    }
}
